package com.app.micaihu.utils;

import android.app.Activity;
import android.content.SharedPreferences;
import android.text.TextUtils;

import com.app.micaihu.app.AppApplication;
import com.app.micaihu.manager.AppConfigManager;
import com.app.utils.util.DateUtils;

import java.util.Map;

/**
 * Created by lenovo on 2016/9/29.
 */
public class HiddenDangerInfoUtils {

    public static String PUSH_CONTENT = "push_content";
    public static String PUSH_TRACE = "push_trace";


    private static HiddenDangerInfoUtils hiddenDangerInfoUtils;
    private static SharedPreferences sharedPreferences;//配置文件
    private String congigFileName = "hiddemdanger_file";

    private HiddenDangerInfoUtils(){}

    public static HiddenDangerInfoUtils getInstance(){
        if(hiddenDangerInfoUtils == null){
            hiddenDangerInfoUtils = new HiddenDangerInfoUtils();
        }
        return hiddenDangerInfoUtils;
    }

    private String date = "---"+ DateUtils.getCurrentTime("yyyy-MM-dd HH:mm:ss")+"---";

    /**
     * 保存记录
     * @param key
     * @param value
     */
    /**
     * 写入配置
     * @param key
     * @param value
     */
    public void writeConfig(String key,String value){

        String currentValue = readConfig(key, "");

        if(sharedPreferences == null){
            sharedPreferences = AppApplication.getInstance().getSharedPreferences(congigFileName, Activity.MODE_PRIVATE);
        }
        if(sharedPreferences == null){
            return;
        }
        SharedPreferences.Editor editor = sharedPreferences.edit();
        if(TextUtils.isEmpty(currentValue)){//如果当前的key没有值，表示第一次
            editor.putString(key,date+value);
        }else{//当前的key 有值
            editor.putString(key,currentValue+","+date+value);
        }
        editor.commit();
    }
    /**
     * 读取配置
     * @param key
     * @param defaultValue
     * @return String
     */
    public String readConfig(String key,String defaultValue){
        if(sharedPreferences == null){
            sharedPreferences = AppApplication.getInstance().getSharedPreferences(congigFileName, Activity.MODE_PRIVATE);
        }
        if(sharedPreferences == null){
            return defaultValue;
        }
        return sharedPreferences.getString(key,defaultValue);
    }



    public void upTrace(String...key){
        StringBuilder sb = new StringBuilder();
        if(sharedPreferences == null){
            sharedPreferences = AppApplication.getInstance().getSharedPreferences(congigFileName, Activity.MODE_PRIVATE);
        }

        Map<String, ?> all = sharedPreferences.getAll();
        if(key != null && key.length > 0){
            for (int i = 0;i<key.length;i++){
                if(all.containsKey(key[i])){
                    String string = (String) all.get(key);
                }
            }
        }

    }

}
